package com.nkzy.mapper;

import com.nkzy.pojo.Address;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface AddressMapper {

    // 插入新的地址信息
    @Insert("INSERT INTO address (userId, province, city, town, mobile, street, contact, isDefault, notes, deleted) " +
            "VALUES (#{userId}, #{province}, #{city}, #{town}, #{mobile}, #{street}, #{contact}, #{isDefault}, #{notes}, #{deleted})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insertAddress(Address address);

    // 根据ID删除地址信息
    @Delete("DELETE FROM address WHERE id = #{id}")
    void deleteAddressById(Long id);

    // 更新地址信息
    @Update("UPDATE address SET userId = #{userId}, province = #{province}, city = #{city}, town = #{town}, " +
            "mobile = #{mobile}, street = #{street}, contact = #{contact}, isDefault = #{isDefault}, notes = #{notes}, deleted = #{deleted} " +
            "WHERE id = #{id}")
    void updateAddress(Address address);

    // 根据ID查询地址信息
    @Select("SELECT * FROM address WHERE id = #{id}")
    Address getAddressById(Long id);

    // 查询所有地址信息
    @Select("SELECT * FROM address")
    List<Address> getAllAddresses();

    // 根据用户ID查询地址信息
    @Select("SELECT * FROM address WHERE userId = #{userId}")
    List<Address> getAddressesByUserId(Long userId);

    // 查询默认地址信息
    @Select("SELECT * FROM address WHERE userId = #{userId} AND isDefault = true")
    Address getDefaultAddressByUserId(Long userId);

    // 根据多个ID查询地址信息
    @Select("<script>" +
            "SELECT * FROM address WHERE id IN " +
            "<foreach item='id' index='index' collection='list' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Address> getAddressesByIds(List<Long> ids);
}